<?php


namespace app\queue\stomp;


use app\queue\BaseConsumer;
use common\service\OriginPlanNodeService;
use Webman\Stomp\RetryAbleConsumer;

/**
 * 填充原计划节点数据
 */
class FixOriginalPlanNodeDataConsumer extends BaseConsumer implements RetryAbleConsumer
{
    // 要消费的队列名
    public $queue = 'fix_original_plan_node_data';

    // 连接名，对应 config/redis_queue.php 里的连接`
    public $connection = 'default';

    /**
     * 消费
     * @param $data
     * @return void
     * @throws \Exception
     */
    public function doConsume($data)
    {
        if (empty($data['start_time']) || empty($data['end_time']) || empty($data['project_id'])) {
            return;
        }
        OriginPlanNodeService::getInstance()->fixOriginalPlanNodeData($data['start_time'], $data['end_time'], $data['project_id'], request()->tenantId);
    }

    /**
     * 重试间隔
     * @return int
     */
    public function getTtr()
    {
        return 20;
    }

    /**
     * 能否重试
     * @param $attempt
     * @param $error
     * @return bool
     */
    public function canRetry($attempt, $error)
    {
        trace(format_exception_info($error), 'ERR');
        return $attempt <= 3;
    }
}
